package com.cpecc.manager.wuzi;

import com.cpecc.dao.wuzi.YiJiaoDao;
import com.cpecc.dao.wuzi.YiJiaoJLDao;
import com.cpecc.liucheng.manager.LiuChengManager;
import com.cpecc.liucheng.model.LiuChengUser;
import com.cpecc.manager.system.HuanJieManager;
import com.cpecc.manager.system.UserManager;
import com.cpecc.model.system.User;
import com.cpecc.model.wuzi.WuZi;
import com.cpecc.model.wuzi.YiJiao;
import com.cpecc.model.wuzi.YiJiaoJL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.*;

@Service
@Transactional
public class YiJiaoManager {

	@Autowired
	private YiJiaoDao yiJiaoDao;

	@Autowired
	private YiJiaoJLDao yiJiaoJLDao;

	@Autowired
	private HuanJieManager huanJieManager;

	@Autowired
	private UserManager userManager;

	@Autowired
	private LiuChengManager liuChengManager;
	
	public void save(YiJiao yiJiao){
		yiJiaoDao.save(yiJiao);
	}
	
	public YiJiao get(String id){
		return yiJiaoDao.get(id);
	}
	
	public boolean cover(YiJiao yiJiao){
		return yiJiaoDao.cover(yiJiao);
	}
	
	public List<YiJiao> getMyTuiHui(User user){
		return yiJiaoDao.getMyTuiHui(user);
	}
	
	public List<YiJiao> getAllTuiHui(){
		return yiJiaoDao.getAllTuiHui();
	}
	
	public List<YiJiao> getMyYiJiao(User user){
		return yiJiaoDao.getMyYiJiao(user);
	}

	public List<YiJiao> getAllYiJiao() {
		return yiJiaoDao.getAllYiJiao();
	}

	public List<YiJiao> getAllYiJiao(int huanJie){
		return yiJiaoDao.getAllYiJiao(huanJie);
	}

	public YiJiao getYiJiaoByWuZi(WuZi wuZi){
		return yiJiaoDao.getYiJiaoByWuZi(wuZi);
	}

	public YiJiao getYiJiaoByWuZi(WuZi wuZi,int huanJie){
		return yiJiaoDao.getYiJiaoByWuZi(wuZi, huanJie);
	}
	
	public List<YiJiao> getMyYiJiaoJS(User user){
		return yiJiaoDao.getMyYiJiaoJS(user);
	}

	public List<YiJiao> getMyYiJiaoRW(User user, Integer huanJieId){
		List<YiJiao> data = new ArrayList<>();

		List<LiuChengUser> list = liuChengManager.list(huanJieId, user.getId());


		if (!CollectionUtils.isEmpty(list)) {
			YiJiao e = null;
			Set<String> idSet = new HashSet<>();
			for (LiuChengUser item : list) {
				if (item.getChuLiJG() != null && item.getChuLiJG().intValue() == 1) {
					continue;
				}

				e = yiJiaoDao.getByLiuChengId(item.getLiuChengId());
				if (e == null) {
					continue;
				}

				if (idSet.contains(e.getId())) {
					continue;
				}

				idSet.add(e.getId());
				data.add(e);
			}
		}
		return data;
	}
	
	public List<YiJiao> getAllYiJiaoByWuZi(WuZi wuZi){
		return yiJiaoDao.getAllYiJiaoByWuZi(wuZi);
	}
	
	public List<YiJiao> getAllTuiHuanByWuZi(WuZi wuZi){
		return yiJiaoDao.getAllTuiHuanByWuZi(wuZi);
	}
	
	public boolean hasWuZi(String wuZiId){
		return yiJiaoDao.hasWuZi(wuZiId);
	}
	
	public boolean hasLiuCheng(User user){
		return yiJiaoDao.hasLiuCheng(user);
	}
	
	public List<YiJiao> activeYiJiao(){
		return yiJiaoDao.activeYiJiao();
	}

	@Transactional
	public void doLiuCheng(String[] yiJiaoId, User currUser) {
		if (yiJiaoId == null || yiJiaoId.length <= 0) {
			return;
		}

		for (String id : yiJiaoId) {
			YiJiao entity = get(id);
			Integer currHuanJieId = entity.getHuanJie().getId();
			Integer nextHuanJieId = getNextHuanJieId(currHuanJieId);

			entity.setHuanJie(huanJieManager.get(nextHuanJieId));
			cover(entity);

			liuChengManager.updateAndSave(entity.getLiuChengId(), currHuanJieId, nextHuanJieId, currUser.getId(), getHuanJieUsers(nextHuanJieId, currUser, entity.getJieShouR()));
			yiJiaoJL(currHuanJieId, huanJieManager.getHuangJieMC(currHuanJieId), null, entity, currUser);
		}
	}

	private Integer getNextHuanJieId(Integer currHuanJieId) {
		if (currHuanJieId == 501) {
			return 507;
		}

		if (currHuanJieId == 508) {
			return 502;
		}
		return currHuanJieId + 1;
	}

	private List<User> getHuanJieUsers(Integer huanJieId, User currUser, User jieShouR) {
		if (huanJieId == 507) {
			return userManager.getBMFZR(currUser);
		}

		if (huanJieId == 508) {
			return userManager.getGKBMFZR();
		}

//		if (huanJieId == 509) {
//			return userManager.getBMFZR(jieShouR);
//		}
		return null;
	}

	private void yiJiaoJL(Integer huanJieId, String piFuLX, String piFuNR, YiJiao yiJiao, User user) {
		YiJiaoJL yiJiaoJL=new YiJiaoJL();
		yiJiaoJL.setCaoZuoRQ(new Date());
		yiJiaoJL.setHuanJie(huanJieManager.get(huanJieId));
		yiJiaoJL.setPiFuLX(piFuLX);
		yiJiaoJL.setPiFuNR(piFuNR);
		yiJiaoJL.setUser(user);
		yiJiaoJL.setYiJiao(yiJiao);
		yiJiaoJLDao.save(yiJiaoJL);
	}

	public boolean delete(String id) {
		return yiJiaoDao.delete(id);
	}

}
